<?php
	define('CONFIG_FILE',true);
	include '../config.php';
	
	define('DB_FILE',true);
	require_once '../Tool/DB/SqlDBManager.class.php';

	session_start();
	if (empty($_SESSION["userType"])) {
		$url="../Index.php";
		include_once '../Tool/Common/Redirect.php';
		exit();
	}
	
	$NRIC = $_REQUEST['NRIC'];
	$customerName = $_REQUEST['CustomerName'];
	$citizenship = $_REQUEST['Citizenship'];
	$daycare = $_REQUEST['Daycare'];
	$beforeDeviation = intval($_REQUEST['BeforeDeviation']);
	$afterDeviation = intval($_REQUEST['AfterDeviation']);
	$dateOfBirth = $_REQUEST['DateOfBirth'];
	$gender = $_REQUEST['Gender'];
	$centreName = $_REQUEST['CentreName'];

	$sqlDBManager = new SqlDBManager();

	$sql="SELECT CentreId from Centre where CentreName=?";
	$parameters=array($centreName);
	$res=$sqlDBManager->queryRow($sql,$parameters);
	if(!empty($res)){
		$centreId=$res['CentreId'];
	}else{
		$centreId=0;
	}

	$sql = "SELECT * from Client where NRIC=?";
	$parameters = array($NRIC);

	$res=$sqlDBManager->queryRow($sql,$parameters);

	if(!empty($res)){
		$NRICExisted=$res['NRIC'];
		$customerNameExisted=$res['CustomerName'];
		$citizenshipExisted=$res['Citizenship'];
		$beforeDeviationExisted=$res['BeforeDeviation'];
		$afterDeviationExisted=$res['AfterDeviation'];
		$dateOfBirthExisted=$res['DateOfBirth'];
		$genderExisted=$res['Gender'];
		$centreIdExisted=$res['CentreId'];
		$daycareExisted=$res['Daycare'];
		//If no changes, return ture else execute update statement
		if($NRIC==$NRICExisted&&$customerName==$customerNameExisted&&$citizenship==$citizenshipExisted&&$beforeDeviation==$beforeDeviationExisted&&$afterDeviation==$afterDeviationExisted&&$dateOfBirth==$dateOfBirthExisted&&$gender==$genderExisted&&$centreId==$centreIdExisted&&$daycare==$daycareExisted){
			echo json_encode(array('success'=>true));	
		}else{

			if($centreId == 0){//Centre has not been updated
				$sql = "UPDATE client SET CustomerName=?,Citizenship=?,Daycare=?,BeforeDeviation=?,AfterDeviation=?, DateOfBirth=?, Gender=? WHERE NRIC=?";
				$parameters = array($customerName,$citizenship,$daycare,$beforeDeviation,$afterDeviation,$dateOfBirth,$gender,$NRIC);
				$res=$sqlDBManager->execute_dml($sql,$parameters);

				//close connection
		        $sqlDBManager->close_connect();

				if ($res>0){
					echo json_encode(array('success'=>true));
				} else {
					echo json_encode(array('msg'=>"Update client failed due to database error."));
				}
			}else{//Centre has been updated
				$sql = "UPDATE client SET CustomerName=?,Citizenship=?,Daycare=?,BeforeDeviation=?,AfterDeviation=?, DateOfBirth=?, Gender=?, CentreId=? WHERE NRIC=?";
				$parameters = array($customerName,$citizenship,$daycare,$beforeDeviation,$afterDeviation,$dateOfBirth,$gender,$centreId,$NRIC);
				$res=$sqlDBManager->execute_dml($sql,$parameters);
				
				//close connection
		        $sqlDBManager->close_connect();

				if ($res>0){
					echo json_encode(array('success'=>true));
				}else {
					echo json_encode(array('msg'=>"Update client failed due to database error."));
				}
			}
		}
	}
?>